//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "RatePseudoRootJacobian.h"
using namespace Cephei::QL::Models::Marketmodels::Pathwisegreeks;
#include <gen/QL/Math/Matrix.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobian::CRatePseudoRootJacobian (Cephei::QL::Math::IMatrix^ pseudoRoot, UInt64 aliveIndex, UInt64 numeraire, Cephei::IVector<Double>^ taus, Cephei::IVector<Cephei::QL::Math::IMatrix^>^ pseudoBumps, Cephei::IVector<Double>^ displacements) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CMatrix^ _CpseudoRoot;
    CoVector<Double>^ _Ctaus;
    CoVector<Cephei::QL::Math::IMatrix^>^ _CpseudoBumps;
    CoVector<Double>^ _Cdisplacements;
    try
    {
#ifdef HANDLE
        _phRatePseudoRootJacobian = NULL;
#endif
        _CpseudoRoot = safe_cast<CMatrix^> (pseudoRoot);
        _CpseudoRoot->Lock();
        QuantLib::Matrix& _pseudoRoot = static_cast<QuantLib::Matrix&> (_CpseudoRoot->GetReference ()); 
        QuantLib::Size _aliveIndex = (QuantLib::Size)ValueHelper::Convert (aliveIndex);
        QuantLib::Size _numeraire = (QuantLib::Size)ValueHelper::Convert (numeraire);
        CoVector<Double>^ _Ctaus = safe_cast<CoVector<Double>^> (taus);
        _Ctaus->Lock();
        INativeVector<Double>^ _NCItaus = _Ctaus->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCtaus = safe_cast<CDoubleVector^>(_NCItaus);
        std::vector<QuantLib::Time>& _taus = static_cast<std::vector<QuantLib::Time>&> (_NCtaus->GetReference ());
        _CpseudoBumps = safe_cast<CoVector<Cephei::QL::Math::IMatrix^>^> (pseudoBumps);
        _CpseudoBumps ->Lock ();
        INativeVector<Cephei::QL::Math::IMatrix^>^ _NCIpseudoBumps = _CpseudoBumps->getFeature (NativeFeature::Value);
        CMatrixVector^ _NCpseudoBumps = safe_cast<CMatrixVector^>(_NCIpseudoBumps);
        std::vector<QuantLib::Matrix>& _pseudoBumps = static_cast<std::vector<QuantLib::Matrix>&> (_NCpseudoBumps->GetReference ());
        CoVector<Double>^ _Cdisplacements = safe_cast<CoVector<Double>^> (displacements);
        _Cdisplacements->Lock();
        INativeVector<Double>^ _NCIdisplacements = _Cdisplacements->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCdisplacements = safe_cast<CDoubleVector^>(_NCIdisplacements);
        std::vector<QuantLib::Spread>& _displacements = static_cast<std::vector<QuantLib::Spread>&> (_NCdisplacements->GetReference ());
        _ppRatePseudoRootJacobian = new boost::shared_ptr<QuantLib::RatePseudoRootJacobian> (new QuantLib::RatePseudoRootJacobian ( _pseudoRoot,  _aliveIndex,  _numeraire,  _taus,  _pseudoBumps,  _displacements ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CpseudoRoot != nullptr) _CpseudoRoot->Unlock();
        if (_Ctaus != nullptr) _Ctaus->Unlock();
        if (_CpseudoBumps != nullptr) _CpseudoBumps->Unlock();
        if (_Cdisplacements != nullptr) _Cdisplacements->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobian::CRatePseudoRootJacobian (boost::shared_ptr<QuantLib::RatePseudoRootJacobian>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRatePseudoRootJacobian = NULL;
#endif
	_ppRatePseudoRootJacobian = &childNative;
    
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobian::CRatePseudoRootJacobian (QuantLib::RatePseudoRootJacobian& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phRatePseudoRootJacobian = NULL;
#endif
	_ppRatePseudoRootJacobian = new boost::shared_ptr<QuantLib::RatePseudoRootJacobian> (&childNative);
    
    _RatePseudoRootJacobianOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobian::CRatePseudoRootJacobian (CRatePseudoRootJacobian^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRatePseudoRootJacobian = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppRatePseudoRootJacobian = new boost::shared_ptr<QuantLib::RatePseudoRootJacobian> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobian::CRatePseudoRootJacobian (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRatePseudoRootJacobian = NULL;
#endif
	if (!t->IsSubclassOf(CRatePseudoRootJacobian::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobian::CRatePseudoRootJacobian (QuantLib::Handle<QuantLib::RatePseudoRootJacobian>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phRatePseudoRootJacobian = &childNative;
	_ppRatePseudoRootJacobian = &static_cast<boost::shared_ptr<QuantLib::RatePseudoRootJacobian>>(childNative.currentLink());
    
    _RatePseudoRootJacobianOwner = owner;
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobian::CRatePseudoRootJacobian (QuantLib::Handle<QuantLib::RatePseudoRootJacobian> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phRatePseudoRootJacobian = &childNative;
	_ppRatePseudoRootJacobian = &static_cast<boost::shared_ptr<QuantLib::RatePseudoRootJacobian>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobian::CRatePseudoRootJacobian (QuantLib::RatePseudoRootJacobian childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRatePseudoRootJacobian = NULL;
#endif
	_ppRatePseudoRootJacobian = new boost::shared_ptr<QuantLib::RatePseudoRootJacobian> (new QuantLib::RatePseudoRootJacobian (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobian::~CRatePseudoRootJacobian ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppRatePseudoRootJacobian != NULL)
    {
	    delete _ppRatePseudoRootJacobian;
        _ppRatePseudoRootJacobian = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobian::!CRatePseudoRootJacobian ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppRatePseudoRootJacobian != NULL)
    {
	    delete _ppRatePseudoRootJacobian;
    }
}
QuantLib::RatePseudoRootJacobian& Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobian::GetReference ()
{
    if (_ppRatePseudoRootJacobian == NULL) throw gcnew NativeNullException ();
	return **_ppRatePseudoRootJacobian;
}
boost::shared_ptr<QuantLib::RatePseudoRootJacobian>& Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobian::GetShared ()
{
    if (_ppRatePseudoRootJacobian == NULL) throw gcnew NativeNullException ();
	return *_ppRatePseudoRootJacobian;
}
QuantLib::RatePseudoRootJacobian* Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobian::GetPointer ()
{
    if (_ppRatePseudoRootJacobian == NULL) throw gcnew NativeNullException ();
	return &**_ppRatePseudoRootJacobian;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::RatePseudoRootJacobian>& Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobian::GetHandle ()
{
	if (_phRatePseudoRootJacobian == NULL)
	{
		_phRatePseudoRootJacobian = new Handle<QuantLib::RatePseudoRootJacobian> (*_ppRatePseudoRootJacobian);
	}
	return *_phRatePseudoRootJacobian;
}
#endif
bool Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobian::HasNative () 
{
	return (_ppRatePseudoRootJacobian != NULL);
}

Cephei::QL::Models::Marketmodels::Pathwisegreeks::IRatePseudoRootJacobian^ Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobian::GetBumps (Cephei::IVector<Double>^ oldRates, Cephei::IVector<Double>^ oneStepDFs, Cephei::IVector<Double>^ newRates, Cephei::IVector<Double>^ gaussians, Cephei::QL::Math::IMatrix^ B)
{
    CoVector<Double>^ _ColdRates;
    CoVector<Double>^ _ConeStepDFs;
    CoVector<Double>^ _CnewRates;
    CoVector<Double>^ _Cgaussians;
    CMatrix^ _CB;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _ColdRates = safe_cast<CoVector<Double>^> (oldRates);
        _ColdRates->Lock();
        INativeVector<Double>^ _NCIoldRates = _ColdRates->getFeature (NativeFeature::Value);
        CDoubleVector^ _NColdRates = safe_cast<CDoubleVector^>(_NCIoldRates);
        std::vector<QuantLib::Rate>& _oldRates = static_cast<std::vector<QuantLib::Rate>&> (_NColdRates->GetReference ());
        _ConeStepDFs = safe_cast<CoVector<Double>^> (oneStepDFs);
        _ConeStepDFs->Lock();
        INativeVector<Double>^ _NCIoneStepDFs = _ConeStepDFs->getFeature (NativeFeature::Value);
        CDoubleVector^ _NConeStepDFs = safe_cast<CDoubleVector^>(_NCIoneStepDFs);
        std::vector<QuantLib::Real>& _oneStepDFs = static_cast<std::vector<QuantLib::Real>&> (_NConeStepDFs->GetReference ());
        _CnewRates = safe_cast<CoVector<Double>^> (newRates);
        _CnewRates->Lock();
        INativeVector<Double>^ _NCInewRates = _CnewRates->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCnewRates = safe_cast<CDoubleVector^>(_NCInewRates);
        std::vector<QuantLib::Rate>& _newRates = static_cast<std::vector<QuantLib::Rate>&> (_NCnewRates->GetReference ());
        _Cgaussians = safe_cast<CoVector<Double>^> (gaussians);
        _Cgaussians->Lock();
        INativeVector<Double>^ _NCIgaussians = _Cgaussians->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCgaussians = safe_cast<CDoubleVector^>(_NCIgaussians);
        std::vector<QuantLib::Real>& _gaussians = static_cast<std::vector<QuantLib::Real>&> (_NCgaussians->GetReference ());
        _CB = safe_cast<CMatrix^> (B);
        _CB->Lock();
        QuantLib::Matrix& _B = static_cast<QuantLib::Matrix&> (_CB->GetReference ()); 
    	(*_ppRatePseudoRootJacobian)->getBumps ( _oldRates,  _oneStepDFs,  _newRates,  _gaussians,  _B );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_ColdRates != nullptr) _ColdRates->Unlock();
        if (_ConeStepDFs != nullptr) _ConeStepDFs->Unlock();
        if (_CnewRates != nullptr) _CnewRates->Unlock();
        if (_Cgaussians != nullptr) _Cgaussians->Unlock();
        if (_CB != nullptr) _CB->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Pathwisegreeks::IRatePseudoRootJacobian^ Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobian_Factory::Create (Cephei::QL::Math::IMatrix^ pseudoRoot, UInt64 aliveIndex, UInt64 numeraire, Cephei::IVector<Double>^ taus, Cephei::IVector<Cephei::QL::Math::IMatrix^>^ pseudoBumps, Cephei::IVector<Double>^ displacements)
{
    return gcnew CRatePseudoRootJacobian ( pseudoRoot,  aliveIndex,  numeraire,  taus,  pseudoBumps,  displacements);
}
